package com.hualan.sfzy.controller.counselor;

import com.hualan.sfzy.entity.counselor.CounselorTalent;
import com.hualan.sfzy.service.counselor.CounselorTalentService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class CounselorTalentController {
    @Autowired
    private CounselorTalentService counselorTalentService;

    @PostMapping("counselorTalent")
    public Result add(@RequestBody CounselorTalent counselorTalent){
        int num = counselorTalentService.add(counselorTalent);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("counselorTalent/{id}")
    public Result delete(@PathVariable Integer id){
        int num = counselorTalentService.remove(id);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("counselorTalent")
    public Result deleteBatch(Integer[] ids){
        int num = counselorTalentService.removeBatch(ids);
        return num > 0 ? Result.success() : Result.error();
    }

    @PutMapping("counselorTalent")
    public Result edit(@RequestBody CounselorTalent counselorTalent){
        int num = counselorTalentService.edit(counselorTalent);
        return num > 0 ? Result.success() : Result.error();
    }

    @GetMapping("counselorTalent")
    public Result find(){
        List<CounselorTalent> list = counselorTalentService.find();
        return Result.success(list);
    }

    @GetMapping("counselorTalent/{id}")
    public Result findById(@PathVariable Integer id){
        CounselorTalent counselorTalent = counselorTalentService.findById(id);
        return Result.success(counselorTalent);
    }

    @GetMapping("/counselorTalent/page")
    public Result findByPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                             CounselorTalent counselorTalent){
        Map<String, Object> map = counselorTalentService.findByPage(pageNum, pageSize, counselorTalent);
        return Result.success(map);
    }

    @PostMapping("/counselorTalent/batch")
    public Result addBatch(@RequestBody List<CounselorTalent> list){
        // 1. 校验是否超过3个
        if (list.size() > 3) {
            return Result.error("最多只能关联3个擅长领域");
        }
        int num = counselorTalentService.addBatch(list);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("counselorTalent/byCounselor/{counselorId}")
    public Result deleteByCounselorId(@PathVariable Integer counselorId) {
        int num = counselorTalentService.removeByCounselorId(counselorId);
        return num > 0 ? Result.success() : Result.error();
    }
}
